#pragma once

#include <iostream>
#include <string>
#include <set>
#include <unistd.h>
#include "InetAddr.hpp"
class Command
{
public:
    Command()
    {
        _WhiteListCommands.insert("ls");
        _WhiteListCommands.insert("pwd");
        _WhiteListCommands.insert("ls -l");
        _WhiteListCommands.insert("touch haha.txt");
        _WhiteListCommands.insert("who");
        _WhiteListCommands.insert("whoami");
    }

    std::string Excute(std::string mes, InetAddr who)
    {
        if (!_WhiteListCommands.count(mes))
            return "None";
        FILE *fp = popen(mes.c_str(), "r");
        if (nullptr == fp)
        {
            return std::string("你要执行的命令不存在: ") + mes;
        }
        std::string res;
        char buffer[1024];
        while (fgets(buffer, sizeof(buffer), fp))
        {
            res += buffer;
        }
        pclose(fp);
        std::string result = who.StringAddr() + "execute done, result is: \n" + res;
        return res;
    }
    ~Command()
    {
    }

private:
    std::set<std::string> _WhiteListCommands;
};